package com.koro.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.koro.dto.SaveShopCartDTO;
import com.koro.entity.ShopCart;
import com.koro.mapper.ShopCartMapper;
import com.koro.service.ShopCartService;
import com.koro.vo.FindShopCartVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ShopCartServiceImpl implements ShopCartService {

    @Autowired
    private ShopCartMapper shopCartMapper;

    @Override
    public Integer save(SaveShopCartDTO dto) {
        ShopCart shopCart = new ShopCart();
        BeanUtils.copyProperties(dto,shopCart);
        if("".equals(dto.getId()) || dto.getId() == 0) {
            shopCartMapper.insert(shopCart);
        } else {
            shopCartMapper.updateById(shopCart);
        }
        return shopCart.getId();
    }

    @Override
    public List<FindShopCartVO> findByUsersId(Integer id) {
        QueryWrapper<ShopCart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("users_id",id);
        queryWrapper.eq("type",0);
        List<ShopCart> list = shopCartMapper.selectList(queryWrapper);
        List<FindShopCartVO> vos = new ArrayList<>();
        list.forEach(s -> {
            FindShopCartVO findShopCartVO = new FindShopCartVO();
            BeanUtils.copyProperties(s,findShopCartVO);
            vos.add(findShopCartVO);
        });

        return vos;
    }
}
